﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace Addresses
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 
    [Flags]
    public enum CTypes
    {
        Friend = 1,
        Business = 2,
        All = 3
    }

    public enum PTypes
    {
        Home = 1,
        Business = 2,
        Mobile = 3
    }


    public partial class MainWindow
    {
        private AddressesEntities4 DB = new AddressesEntities4();
        private Entry[] selectedEntries;
        private Email[] selectedEmails;
        private Phone[] selectedPhone;
        private int current;
        private int currEmail;
        private int currPhone;
        private int origRecord = -1;

        private CTypes currentCategory = CTypes.All;

        public MainWindow()
        {
            InitializeComponent();
            SelectAllEntries();
            Display();
        }

        private void SelectAllEntries()
        {
            //SelectedEntries = AddressList.ToArray();
            selectedEntries =
                (from e in DB.Entries
                 where ((int)((CTypes)e.ContactType & currentCategory) > 0)
                 orderby e.Name
                 select e).ToArray();
        }


        private void Display()
        {
            Entry e = selectedEntries[current];
            if (current != origRecord)
            {
                origRecord = current;
                currEmail = 0;
                currPhone = 0;
                selectedEmails = e.Emails.ToArray();
                selectedPhone = e.Phones.ToArray();
                tbName.Text = e.Name;
                tbAddress.Text = e.Address;
                tbCSZ.Text = e.CSZ;
                phoneType.Text = ((PTypes)selectedPhone[currPhone].PhoneType).ToString();
                if (e.ContactType != null) tbRelationship.Text = ((CTypes)e.ContactType).ToString();
                btnStart.IsEnabled = btnPrevious.IsEnabled = (current != 0);
                btnNext.IsEnabled = btnEnd.IsEnabled = (current < selectedEntries.Length - 1);



                //lblLocation.Content = "Record " + (current+1) + " of " + selectedEntries.Length;
            }

            tbEmail.Text = selectedEmails[currEmail].EmailAddress;
            tbPhone.Text = selectedPhone[currPhone].Phone1;

        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            current = 0;
            Display();
        }

        private void btnPrevious_Click(object sender, RoutedEventArgs e)
        {
            current = 9;
            Display();

        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            if (current <= 0) return;
            current--;
            Display();
        }

        private void btnEnd_Click(object sender, RoutedEventArgs e)
        {
            if (current >= 9) return;
            current++;
            Display();
        }

        private void btnFind_Click(object sender, RoutedEventArgs e)
        {
            if (tbSearch.Text == "")
            {
                SelectAllEntries();
                current = 0;
                Display();
            }
            var found = from ent in DB.Entries
                        where ent.Name.Contains(tbSearch.Text) &&
                              ((int)((CTypes)ent.ContactType & currentCategory) > 0)
                        select ent;
            if (!found.Any())
            {
                MessageBox.Show("No Records Found containing " + tbSearch.Text);
                return;
            }
            selectedEntries = found.ToArray();
            current = 0;
            Display();
        }

        private void rb_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton button = (RadioButton)sender;
            if (button == rbAll) currentCategory = CTypes.All;
            if (button == rbFriends) currentCategory = CTypes.Friend;
            if (button == rbBusiness) currentCategory = CTypes.Business;
            tbSearch.Text = "";
            SelectAllEntries();
            current = 0;
            Display();


        }

        private void nextEmail_Click(object sender, RoutedEventArgs e)
        {
            currEmail++;
            if (currEmail >= selectedEmails.Length)
            {
                currEmail = 0;
            }
            Display();
        }

        private void addEmail_Click(object sender, RoutedEventArgs e)
        {
            AddEmail aem = new AddEmail();

            if (aem.ShowDialog() == true)
            {
                Email rec = new Email();
                rec.ID = selectedEntries[current].ID;
                rec.EmailAddress = aem.EmailAddress;
                selectedEntries[current].Emails.Add(rec);
                DB.SaveChanges();
                selectedEmails = selectedEntries[current].Emails.ToArray();
                currEmail = selectedEmails.Length - 1;
                Display();

            }
        }

        private void nextPhone_Click(object sender, RoutedEventArgs e)
        {
            currPhone++;
            if (currPhone >= selectedPhone.Length)
            {
                currPhone = 0;
            }
            phoneType.Text = ((PTypes)selectedPhone[currPhone].PhoneType).ToString();
            Display();
        }

        private void addPhone_Click(object sender, RoutedEventArgs e)
        {
            AddPhone addPhone = new AddPhone();

            if (addPhone.ShowDialog() == true)
            {
                Phone rec = new Phone();
                rec.ID = selectedEntries[current].ID;
                rec.Phone1 = addPhone.PhoneNumber;
                rec.PhoneType = addPhone.PhoneType;
                selectedEntries[current].Phones.Add(rec);
                DB.SaveChanges();
                selectedPhone = selectedEntries[current].Phones.ToArray();
                currPhone = selectedPhone.Length - 1;
                Display();

            }

        }
    }
}